package cn.itcast.jx.action.cargo;

import java.io.ByteArrayOutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.domain.ContractProduct;
import cn.itcast.jx.service.ContractProductService;
import cn.itcast.jx.util.DownloadUtil;
import cn.itcast.jx.util.UtilFuns;

import com.itextpdf.awt.AsianFontMapper;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class OutProductAction extends BaseAction {

	private String inputDate;
	private ContractProductService contractProductService;
	


	
	public String toedit() throws Exception {
		return "toedit";
	}
	
	
	
	public String printPDF() throws Exception {
		//创建文档
		Document document = new Document(PageSize.A4, 20, 20, 20, 20);
		//设置输出位置(输出流)
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		PdfWriter.getInstance(document, byteArrayOutputStream);
		
		//打开文档
		document.open();
		//写入内容
		//支持中文
		BaseFont baseFont = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, 
				AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
		/***********标题***********/
		//设置字体
		Font titleFont = new Font(baseFont	, 30f, Font.BOLD, BaseColor.PINK);
		//设置内容
		Paragraph titleParagraph = new Paragraph("出货表", titleFont);
		//设置对其方式
		titleParagraph.setAlignment(Paragraph.ALIGN_CENTER);
		
		document.add(titleParagraph);
		/***********内容*********/
		//创建表格
		PdfPTable table = new PdfPTable(8);
		//设置表格的上边距
		table.setSpacingBefore(20f);
		
		//标题
		Font tableTitleFont = new Font(baseFont, 12f, Font.BOLD, BaseColor.BLACK);
		
		String[] titles={"客户","订单号","货号","数量","工厂","交期","船期","贸易条款"};
		
		for(String title:titles){
			table.addCell(new PdfPCell(new Phrase(title, tableTitleFont)));
		}
		
		//正式数据
		
		String hql = "from ContractProduct where to_char(contract.shipTime,'YYYY-MM')='"+inputDate+"'";
		
		List<ContractProduct> list = contractProductService.find(hql, ContractProduct.class, null);
		
		//准备字体
		Font contentFont = new Font(baseFont, 12f, Font.NORMAL, BaseColor.BLACK);
		
		for(ContractProduct cp:list){
			
			
			table.addCell(new PdfPCell(new Phrase(cp.getContract().getCustomName(), contentFont)));
			table.addCell(new PdfPCell(new Phrase(cp.getContract().getContractNo(), contentFont)));
			table.addCell(new PdfPCell(new Phrase(cp.getProductNo(), contentFont)));
			table.addCell(new PdfPCell(new Phrase(cp.getCnumber()+"", contentFont)));
			table.addCell(new PdfPCell(new Phrase(cp.getFactoryName(), contentFont)));
			table.addCell(new PdfPCell(new Phrase(UtilFuns.dateTimeFormat(cp.getContract().getDeliveryPeriod()), contentFont)));
			table.addCell(new PdfPCell(new Phrase(UtilFuns.dateTimeFormat(cp.getContract().getShipTime()), contentFont)));
			table.addCell(new PdfPCell(new Phrase(cp.getContract().getTradeTerms(), contentFont)));
		}
		//将表格添加至文档中
		document.add(table);
		//关闭文档
		document.close();
		//下载
		HttpServletResponse response = ServletActionContext.getResponse();
		
		DownloadUtil downloadUtil = new DownloadUtil();
		downloadUtil.download(byteArrayOutputStream, response, "出货表.pdf");
		
		return NONE;
			
		

	}
	
	
	
	
	
	
	
	/*public String print() throws Exception {
		//模板打印
		//模板路径
		String path = ServletActionContext.getRequest().getRealPath("/make/xlsprint/tOUTPRODUCT.xls");
		
		FileInputStream fis = new FileInputStream(new File(path));
		
		String hql = "from ContractProduct where to_char(contract.shipTime,'yyyy-mm') = ?";
		List<ContractProduct> list = contractProductService.find(hql, ContractProduct.class, new String[]{inputDate});
		
		//1 创建工作簿
		Workbook wb = new HSSFWorkbook(fis);
		//2 创建工作表
		//Sheet sheet = wb.createSheet();
		Sheet sheet = wb.getSheetAt(0);
		//定义公共变量
		Row nRow = null;//行对象
		Cell nCell = null;//单元格对象
		
		int rowNo = 0;//第几行
		int cellNo = 1;//列对象
		
		//设置列宽 第一个参数：第n列，第二个参数：列宽
		
		
		*//*******************设置大标题********************//*
		//3 创建行对象
		nRow = sheet.getRow(rowNo);
		//4 创建单元格
		nCell = nRow.getCell(cellNo);
		
		//行高+合并单元格
		nRow.setHeightInPoints(36f);
		
		//设置内容：
		*//**
		 * 2012-01:
		 * 2012-10: 
		 * 方式一：inputDate.replace("-0","-").replace("-","年")
		 * 方式二：inputDate.replace("-0","年").replace("-","年")
		 *//*
		nCell.setCellValue(inputDate.replace("-0", "-").replace("-", "年")+"月份出货表");
		// 6 设置样式
		*//*******************设置小标题********************//*
		rowNo ++;
		
		*//*******************设置出货数据********************//*
		
		rowNo++;//进入数据第一行行
		//获取样式
		nRow = sheet.getRow(rowNo);
		
		//客户	
		CellStyle customerCellStyle = nRow.getCell(cellNo++).getCellStyle();
		//订单号	
		CellStyle contractNoCellStyle = nRow.getCell(cellNo++).getCellStyle();
		//货号	
		CellStyle productNoCellStyle = nRow.getCell(cellNo++).getCellStyle();
		//数量	
		CellStyle cnumberCellStyle = nRow.getCell(cellNo++).getCellStyle();
		//工厂	
		CellStyle factoryCellStyle = nRow.getCell(cellNo++).getCellStyle();
		//工厂交期	
		CellStyle deliveryPeriodCellStyle = nRow.getCell(cellNo++).getCellStyle();
		//船期	
		CellStyle shipTimeCellStyle = nRow.getCell(cellNo++).getCellStyle();
		//贸易条款
		CellStyle tradeTermsCellStyle = nRow.getCell(cellNo++).getCellStyle();

		
		for (ContractProduct cp : list) {
			//单元格no归1
			cellNo = 1;
			
			//一条数据创建一行
			nRow = sheet.createRow(rowNo);
			//在创建每列的数据
			//客户	
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getContract().getCustomName());
			nCell.setCellStyle(customerCellStyle);
			
			//订单号	
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getContract().getContractNo());
			nCell.setCellStyle(contractNoCellStyle);
			//货号	
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getProductNo());
			nCell.setCellStyle(productNoCellStyle);
			//数量	
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getCnumber());
			nCell.setCellStyle(cnumberCellStyle);
			//工厂	
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getFactoryName());
			nCell.setCellStyle(factoryCellStyle);
			//工厂交期	
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(UtilFuns.dateTimeFormat(cp.getContract().getDeliveryPeriod()));
			nCell.setCellStyle(deliveryPeriodCellStyle);
			
			//船期	
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(UtilFuns.dateTimeFormat(cp.getContract().getShipTime()));
			nCell.setCellStyle(shipTimeCellStyle);
			//贸易条款
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getContract().getTradeTerms());
			nCell.setCellStyle(tradeTermsCellStyle);
			
			
//			开始操作数据，rowNo++
			rowNo++;

		}
		
		
		// 7 写入流
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//字节数组缓冲流
		wb.write(byteArrayOutputStream);
		
		// 8下  载
		DownloadUtil downloadUtil = new DownloadUtil();
		HttpServletResponse response = ServletActionContext.getResponse();
		*//**
		 * 第一个参数：流
		 * 第二个参数：response
		 * 第三个参数：下载的文件名
		 *//*
		downloadUtil.download(byteArrayOutputStream, response, "出货表.xls");
		return NONE;
	}
	
	
	
	

	public String print1() throws Exception {
		String hql = "from ContractProduct where to_char(contract.shipTime,'yyyy-mm') = ?";
		List<ContractProduct> list = contractProductService.find(hql, ContractProduct.class, new String[]{inputDate});
		
		//1 创建工作簿
		Workbook wb = new HSSFWorkbook();
		//2 创建工作表
		Sheet sheet = wb.createSheet();
		
		//定义公共变量
		Row nRow = null;//行对象
		Cell nCell = null;//单元格对象
		
		int rowNo = 0;//第几行
		int cellNo = 1;//列对象
		
		//设置列宽 第一个参数：第n列，第二个参数：列宽
		sheet.setColumnWidth(0, 6*256);
		sheet.setColumnWidth(1, 26*256);
		sheet.setColumnWidth(2, 12*256);
		sheet.setColumnWidth(3, 30*256);
		sheet.setColumnWidth(4, 12*256);
		sheet.setColumnWidth(5, 15*256);
		sheet.setColumnWidth(6, 10*256);
		sheet.setColumnWidth(7, 10*256);
		sheet.setColumnWidth(8, 9*256);
		
		*//*******************设置大标题********************//*
		//3 创建行对象
		nRow = sheet.createRow(rowNo);
		//4 创建单元格
		nCell = nRow.createCell(cellNo);
		
		//行高+合并单元格
		nRow.setHeightInPoints(36f);
		
		//横向合并单元格
		sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));
		
		// 5 设置数据
		//设置内容：
		*//**
		 * 2012-01:
		 * 2012-10: 
		 * 方式一：inputDate.replace("-0","-").replace("-","年")
		 * 方式二：inputDate.replace("-0","年").replace("-","年")
		 *//*
		nCell.setCellValue(inputDate.replace("-0", "-").replace("-", "年")+"月份出货表");
		// 6 设置样式
		nCell.setCellStyle(bigTitle(wb));
		*//*******************设置小标题********************//*
		rowNo ++;
		String[] arr= {"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
		
		nRow = sheet.createRow(rowNo);
		nRow.setHeightInPoints(26.25f);
		for (String title : arr) {
			nCell = nRow.createCell(cellNo);
			nCell.setCellStyle(title(wb));
			nCell.setCellValue(title);
			cellNo++;
		}
		
		*//*******************设置出货数据********************//*
		
		nRow.setHeightInPoints(26.25f);
		
		for (ContractProduct cp : list) {
			rowNo ++;
			nRow = sheet.createRow(rowNo);
			cellNo = 1;
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getContract().getCustomName());
			nCell.setCellStyle(text(wb));
			
			
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getContract().getContractNo());
			nCell.setCellStyle(text(wb));
			
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getProductNo());
			nCell.setCellStyle(text(wb));
			
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getCnumber());
			nCell.setCellStyle(text(wb));
			
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getFactory().getFactoryName());
			nCell.setCellStyle(text(wb));
			
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(UtilFuns.dateTimeFormat(cp.getContract().getDeliveryPeriod()));
			nCell.setCellStyle(text(wb));
			
			
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(UtilFuns.dateTimeFormat(cp.getContract().getShipTime()));
			nCell.setCellStyle(text(wb));
			
			nCell = nRow.createCell(cellNo++);
			nCell.setCellValue(cp.getContract().getTradeTerms());
			nCell.setCellStyle(text(wb));
		}
		
		
		// 7 写入流
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//字节数组缓冲流
		wb.write(byteArrayOutputStream);
		
		// 8下  载
		DownloadUtil downloadUtil = new DownloadUtil();
		HttpServletResponse response = ServletActionContext.getResponse();
		*//**
		 * 第一个参数：流
		 * 第二个参数：response
		 * 第三个参数：下载的文件名
		 *//*
		downloadUtil.download(byteArrayOutputStream, response, "a.xls");
		return NONE;
	}*/
	

	//大标题的样式
/*	public CellStyle bigTitle(Workbook wb){
		CellStyle style = wb.createCellStyle();
		Font font = wb.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short)16);
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);					//字体加粗
		
		style.setFont(font);
		
		style.setAlignment(CellStyle.ALIGN_CENTER);					//横向居中
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);		//纵向居中
		
		return style;
	}
	//小标题的样式
	public CellStyle title(Workbook wb){
		CellStyle style = wb.createCellStyle();
		Font font = wb.createFont();
		font.setFontName("黑体");
		font.setFontHeightInPoints((short)12);
		
		style.setFont(font);
		
		style.setAlignment(CellStyle.ALIGN_CENTER);					//横向居中
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);		//纵向居中
		
		style.setBorderTop(CellStyle.BORDER_THIN);					//上细线
		style.setBorderBottom(CellStyle.BORDER_THIN);				//下细线
		style.setBorderLeft(CellStyle.BORDER_THIN);					//左细线
		style.setBorderRight(CellStyle.BORDER_THIN);				//右细线
		
		return style;
	}
	
	//文字样式
	public CellStyle text(Workbook wb){
		CellStyle style = wb.createCellStyle();
		Font font = wb.createFont();
		font.setFontName("Times New Roman");
		font.setFontHeightInPoints((short)10);
		
		style.setFont(font);
		
		style.setAlignment(CellStyle.ALIGN_LEFT);					//横向居左
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);		//纵向居中
		
		style.setBorderTop(CellStyle.BORDER_THIN);					//上细线
		style.setBorderBottom(CellStyle.BORDER_THIN);				//下细线
		style.setBorderLeft(CellStyle.BORDER_THIN);					//左细线
		style.setBorderRight(CellStyle.BORDER_THIN);				//右细线
		
		return style;
	}
	
 */
	
	
	public void setInputDate(String inputDate) {
		this.inputDate = inputDate;
	}

	public String getInputDate() {
		return inputDate;
	}
	public void setContractProductService(
			ContractProductService contractProductService) {
		this.contractProductService = contractProductService;
	}
}
